package mortal.game.control
{
    import Message.Db.Tables.*;
    import Message.Game.*;
    import Message.Public.*;
    import com.gengine.global.*;
    import flash.events.*;
    import flash.geom.*;
    import modules.*;
    import mortal.game.events.*;
    import mortal.game.manager.*;
    import mortal.game.model.*;
    import mortal.game.mvc.*;
    import mortal.game.resource.info.*;
    import mortal.game.rules.*;
    import mortal.game.scene.events.*;
    import mortal.game.scene.map.*;
    import mortal.game.scene.player.entity.*;
    import mortal.game.view.shop.info.*;
    import mortal.mvc.core.*;
    import mortal.mvc.interfaces.*;

    public class ShopController extends Controller
    {
        private var _shopModule:ShopModule;
        private var _honorShopModule:HonorShopModule;
        private var _normalShop:NormalShopModule;
        private var _shopCode:int = 0;

        public function ShopController()
        {
            Dispatcher.addEventListener(EventName.ShopOpen, this.onShopOpenHandler);
            Dispatcher.addEventListener(EventName.ShopClose, this.onShopCloseHandler);
            Dispatcher.addEventListener(EventName.ShopOpenHonor, this.onShopOpenHonorHandler);
            Dispatcher.addEventListener(EventName.VIPShopOpen, this.onVIPShopOpenHandler);
            Dispatcher.addEventListener(EventName.ShopOpenNormal, this.onShopOpenNormalHandler);
            Dispatcher.addEventListener(EventName.ShopBuyItem, this.buyItemHandler);
            Dispatcher.addEventListener(EventName.ShopSellItem, this.sellItemHandler);
            Dispatcher.addEventListener(EventName.ShopSellItemSuccess, this.ShopSellItemSuccessHandler);
            Dispatcher.addEventListener(EventName.ShopFixEquipment, this.fixEquipmentHandler);
            Dispatcher.addEventListener(EventName.ShopFixAllEquipment, this.fixAllEquipmentHandler);
            Dispatcher.addEventListener(EventName.ShopFixEquipmentSuccess, this.fixEquipmentSuccessHandler);
            Dispatcher.addEventListener(EventName.ShopFixAllEquipmentSuccess, this.fixAllEquipmentSuccessHandler);
            Dispatcher.addEventListener(EventName.ShopSellList, this.onSellListHandler);
            Dispatcher.addEventListener(EventName.ShopBuyBack, this.onBuyBackHandler);
            Dispatcher.addEventListener(EventName.RepairAllWeaponCost, this.onRepairAllWeaponCost);
            
        }

        override public function get view() : IView
        {
            return this.initView();
        }

        override protected function initView() : IView
        {
            if (this._shopModule == null)
            {
                this._shopModule = new ShopModule();
                this._shopModule.addEventListener(Event.ADDED_TO_STAGE, this.onAddedToStageHandler);
            }
            return this._shopModule;
        }

        private function onAddedToStageHandler(event:Event) : void
        {
            this._shopModule.defaultSelectBuyBtn();
            
        }

        override protected function initServer() : void
        {
            NetDispatcher.addCmdListener(ServerCommand.MoneyUpdate, this.onMoneyUpdateHandler);
            
        }

        private function onMoneyUpdateHandler(param1:SMoney = null) : void
        {
            if (this._shopModule && this._shopModule.isHide == false)
            {
                this._shopModule.update(cache.role.money);
            }
            
        }

        private function buyItemHandler(event:DataEvent) : void
        {
            var _loc_2:int = 0;
            if (!cache.vip.isVIP)
            {
                _loc_2 = cache.dialog.npcInfo.tnpc.npcId;
            }
            var _loc_3:* = event.data["itemInfo"] as ShopItemInfo;
            var _loc_4:* = event.data["amount"] as int;
            GameProxy.shopProxy.buyItem(_loc_2, _loc_3.tshopSell.shopCode, _loc_3.itemInfo.item.code, _loc_4);
            
        }

        private function sellItemHandler(event:DataEvent) : void
        {
            var _loc_2:int = 0;
            if (!cache.vip.isVIP)
            {
                _loc_2 = cache.dialog.npcInfo.tnpc.npcId;
            }
            var _loc_3:Array = [];
            var _loc_4:* = event.data as String;
            _loc_3.push(_loc_4);
            GameProxy.shopProxy.sellItem(_loc_2, _loc_3);
            
        }

        private function ShopSellItemSuccessHandler(event:DataEvent) : void
        {
            Dispatcher.dispatchEvent(new DataEvent(EventName.ShopSellList));
            
        }

        private function fixEquipmentHandler(event:DataEvent) : void
        {
            var _loc_5:ItemInfo = null;
            var _loc_6:Object = null;
            var _loc_7:TItemWeapon = null;
            var _loc_2:* = event.data["posType"] as int;
            var _loc_3:* = event.data["itemData"] as ItemData;
            var _loc_4:* = cache.dialog.npcInfo.tnpc.npcId;
            if (_loc_3)
            {
                _loc_5 = _loc_3.itemInfo;
                _loc_6 = _loc_3.itemExInfo;
                if (_loc_5 && _loc_5.extend && _loc_5.extend is TItemWeapon)
                {
                    _loc_7 = _loc_5.extend as TItemWeapon;
                }
                if (_loc_6)
                {
                    if (_loc_6.durable != _loc_7.durableUpper || _loc_7.durable != _loc_7.durableUpper)
                    {
                        GameProxy.shopProxy.repairWeapon(ERepairType.ERepairTypeSingle, _loc_2, _loc_3.uid, _loc_4);
                    }
                    else
                    {
                        MsgManager.addBroadCast("装备不需要修理");
                        
                    }
                }
            }
            
        }

        private function fixEquipmentSuccessHandler(event:DataEvent) : void
        {
            MsgManager.addBroadCast("修理装备成功");
            
        }

        private function fixAllEquipmentSuccessHandler(event:DataEvent) : void
        {
            MsgManager.addBroadCast("修理全部装备成功");
            
        }

        private function fixAllEquipmentHandler(event:DataEvent) : void
        {
            var _loc_2:int = 0;
            if (cache.dialog.npcInfo == null)
            {
                _loc_2 = 0;
            }
            else
            {
                _loc_2 = cache.dialog.npcInfo.tnpc.npcId;
            }
            GameProxy.shopProxy.repairWeapon(ERepairType.ERepairTypeAll, 0, "", _loc_2);
            
        }

        private function onSellListHandler(event:DataEvent) : void
        {
            GameProxy.shopProxy.sellList();
            
        }

        private function onBuyBackHandler(event:DataEvent) : void
        {
            var _loc_2:* = event.data as SPlayerItem;
            GameProxy.shopProxy.buyBack([_loc_2.uid]);
            
        }

        private function onShopOpenHandler(event:DataEvent) : void
        {
            var _loc_5:int = 0;
            var _loc_2:* = (Global.stage.stageHeight - GameController.pack.view.height) / 2;
            var _loc_3:* = event.data["shopCode"] as int;
            var _loc_4:* = event.data["shopType"] as int;
            (this.view as ShopModule).updateShopTabData(_loc_4, _loc_3);
            if (_loc_4 == NormalShopType.ExchangeShop)
            {
                _loc_5 = cache.shop.getDefExchangeShopTabIndex();
            }
            this._shopModule.showDefaultTab(_loc_5);
            this.view.show(380, _loc_2);
            Dispatcher.dispatchEvent(new DataEvent(EventName.ShopSellList));
            GameController.pack.view.show(720, _loc_2);
            cache.shop.isOpening = true;
            this._shopModule.guideCheck();
            NpcEffectRule.registDialogEffectWindow(this.view);
            NpcEffectRule.registDialogEffectWindow(GameController.pack.view);
            
        }

        private function onVIPShopOpenHandler(event:DataEvent) : void
        {
            var _loc_2:int = 1000;
            Dispatcher.dispatchEvent(new DataEvent(EventName.ShopOpen, {shopCode:_loc_2, shopType:NormalShopType.DrugShop}));
            
        }

        private function onShopOpenNormalHandler(event:DataEvent) : void
        {
            var _loc_2:* = event.data as int;
            var _loc_3:* = (Global.stage.stageHeight - GameController.pack.view.height) / 2;
            if (this._normalShop == null)
            {
                this._normalShop = new NormalShopModule();
            }
            this._normalShop.setDefaultPage(_loc_2);
            this._normalShop.show(380, _loc_3);
            GameController.pack.view.show(720, _loc_3);
            
        }

        private function onShopOpenHonorHandler(event:DataEvent) : void
        {
            var _loc_2:* = event.data as int;
            var _loc_3:* = (Global.stage.stageHeight - GameController.pack.view.height) / 2;
            if (this._honorShopModule == null)
            {
                this._honorShopModule = new HonorShopModule();
            }
            this._honorShopModule.setSelectedBtn();
            this._honorShopModule.setDefaultPage(_loc_2);
            var _loc_4:* = cache.shop.getDefExchangeShopTabIndex();
            this._honorShopModule.showDefaultTab(_loc_4);
            this._honorShopModule.show(380, _loc_3);
            GameController.pack.view.show(710, _loc_3);
            NpcEffectRule.registDialogEffectWindow(this._honorShopModule);
            NpcEffectRule.registDialogEffectWindow(GameController.pack.view);
            
        }

        private function onShopCloseHandler(event:DataEvent) : void
        {
            cache.shop.isOpening = false;
            cache.shop.operType = "";
            
        }

        private function onChangeSceneHandler(event:DataEvent) : void
        {
            if (this._shopModule && this._shopModule.isHide == false)
            {
                this._shopModule.hide();
            }
            if (this._honorShopModule && this._honorShopModule.isHide == false)
            {
                this._honorShopModule.hide();
            }
            
        }

        private function onGirdWalkEnd(event:PlayerEvent) : void
        {
            var _loc_3:NPCInfo = null;
            var _loc_4:Point = null;
            var _loc_5:Number = NaN;
            var _loc_2:* = RolePlayer.instance.currentPoint;
            if (this.view.isHide == false || this._honorShopModule && this._honorShopModule.isHide == false)
            {
                _loc_3 = cache.dialog.npcInfo;
                if (_loc_3)
                {
                    _loc_4 = GameMapUtil.getPixelPoint(_loc_3.snpc.point.x, _loc_3.snpc.point.y);
                    _loc_5 = Math.sqrt(Math.pow(_loc_4.x - _loc_2.x, 2) + Math.pow(_loc_4.y - _loc_2.y, 2));
                    if (_loc_5 > 120)
                    {
                        this.view.hide();
                        if (this._honorShopModule)
                        {
                            this._honorShopModule.hide();
                        }
                    }
                }
            }
            
        }

        private function onRepairAllWeaponCost(event:DataEvent) : void
        {
            GameProxy.shopProxy.repairAllWeaponCost();
            
        }

    }
}
